import {
    reqProductModelPage,
    reqBatchClosedLoop,
    reqOrgList,
} from "@api/api.js";

export default {
    data() {
        return {
            //实现时间限制选择在一个月内而且不能超过当前时间
            pickerOptions: {
                onPick: ({ maxDate, minDate }) => {
                    this.cuttentTime = minDate.getTime();
                    if (maxDate) {
                        this.cuttentTime = "";
                    }
                },
                disabledDate: (time) => {
                    //获取当前时间戳
                    let myDate = new Date();
                    let newY = myDate.getFullYear();
                    let newM = myDate.getMonth() + 1;
                    let newD = myDate.getDate();
                    let oldTime =
                        newY +
                        "-" +
                        (newM < 10 ? "0" + newM : newM) +
                        "-" +
                        (newD < 10 ? "0" + newD : newD) +
                        " " +
                        "23:59:59";
                    // let oldTime="2022-7-5 23:59:59"
                    oldTime = new Date(oldTime).getTime(); // 将中国时间转换成毫秒
                    // 今天之后的时间不可选
                    if (time.getTime() > oldTime) {
                        //time.getTime() > Date.now()
                        return true;
                    }
                    //选择日期范围为一个月
                    if (this.cuttentTime != "") {
                        const one = 30 * 24 * 3600 * 1000;
                        const minTime = this.cuttentTime - one;
                        const maxTime = this.cuttentTime + one;
                        return time.getTime() < minTime || time.getTime() > maxTime;
                    }
                },
            },
            statusList: [{
                    id: 1,
                    name: "未处理",
                },
                {
                    id: 3,
                    name: "修复中",
                },
                {
                    id: 4,
                    name: "已修复",
                },
                {
                    id: 2,
                    name: "已忽略",
                },
            ],
            sourceList: [{
                    id: 1,
                    name: "人工上报",
                },
                {
                    id: 0,
                    name: "设备上报",
                },
            ],
            deviceModelList: [],
            projectList: [], //单位数据
            loading: false,
            sysInstanceIndex: 1,
            sysInstanceTotalPages: 10,
            flag: true,
            orgName: "",
            selectedValue: "",
        };
    },
    components: {},
    mounted() {},
    methods: {
        // 搜索
        async searchClick1() {
            this.page = 1;
            this.limit = 10;
            this.showPopover = false;
            await this.hiddenDangerStaticsList();
        },
        // 高级搜索重置
        async resetClick1() {
            this.timeArr = [];
            this.search_val.tenantId = "";
            this.search_val.orgId = "";
            this.search_val.startTime = "";
            this.search_val.endTime = "";
            this.search_val.type = "";
            this.search_val.deviceType = "";
            this.search_val.deviceModel = "";
            this.search_val.order = "";
            this.search_val.source = "";

            this.deviceModelList = [];
            this.showPopover = false;
            this.sysInstanceIndex2 = 1;
            this.sysInstanceTotalPages2 = 10;
            this.sysInstanceIndexUnit = 1;
            this.sysInstanceUnitTotalPages = 10;

            this.page = 1;
            this.limit = 10;
            await this.getPreMonth();
            await this.hiddenDangerStaticsList();
            //   this.handleClear();
        },
        // 清空
        handleClear() {
            this.orgName = "";
            this.selectedValue = "";
            this.sysInstanceIndex = 1;
            this.sysInstanceUnitTotalPages = 10;
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            this.projectList = [];
            this.OrgList();
        },
        // 远程搜索租户
        async remoteMethodEnterprise(query) {
            console.log(query, "query");
            this.tenantName = query;
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            this.enterpriseList = [];
            this.getEnterpriseList();
        },
        // 租户下拉框隐藏
        visibleChangeEnterprise(e) {
            if (!e && !this.search_val.tenantId) {
                this.tenantName = "";
                this.enterpriseList = [];
                this.sysInstanceIndex = 1;
                this.getEnterpriseList();
            }
        },
        // 远程搜索
        async remoteMethod(query) {
            if (!this.search_val.tenantId)
                return this.$modal.notifyWarning("请先选择租户");
            this.selectedValue = query; //保留输入的值
            this.orgName = query;
            this.sysInstanceIndexUnit = 1;
            this.sysInstanceUnitTotalPages = 10;
            this.projectList = [];
            this.OrgList();
        },
        // 选择单位
        changeOrgId(e) {
            this.search_val.orgId = e;
        },
        // 下拉请求列表
        enterpriseChange(val) {
            if (val && this.flag) {
                this.OrgList();
                this.flag = false; //只执行一次
            }
        },
        // 获取单位列表
        async OrgList() {
            var res = await reqOrgList({
                userId: this.user.userId,
                roleName: this.user.roleName,
                // tenantId: this.user.tenantId,
                tenantId: this.search_val.tenantId,
                orgName: this.orgName,
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.projectList = res.data.rows
        },
        // 产品类型
        async deviceTypeChange(e) {
            this.productList.forEach((item) => {
                if (item.deviceType == e) {
                    this.productId = item.id;
                    return;
                }
            });
            this.classMarkFlag = true;
            this.deviceModelList = [];
            this.sysInstanceIndex2 = 1;
            this.sysInstanceTotalPages2 = 10;
            this.search_val.classMark = "";
            this.search_val.deviceModel = "";
            this.ProductModelPage();
        },
        // 获取产品型号
        async ProductModelPage() {
            this.deviceModelList = [];
            let res = await reqProductModelPage(this.sysInstanceIndex2, 10, {
                productId: this.productId,
            });
            // console.log(res);
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.sysInstanceTotalPages2 = res.data.total;
            this.deviceModelList = [...this.deviceModelList, ...res.data.rows];
        },
        async loadMoreUnit() {
            if (
                this.sysInstanceIndexUnit >=
                Math.ceil(this.sysInstanceUnitTotalPages / 10)
            )
                return;
            this.sysInstanceIndexUnit++;
            console.log(this.sysInstanceIndexUnit, "this.sysInstanceIndex");
            this.loading = true;
            // 获取列表数据的方法
            await this.OrgList();
            this.loading = false;
        },
        // 清空租户选框时
        onClearEnterprise() {
            this.sysInstanceIndex = 1;
            this.tenantName = "";
            this.search_val.orgId = "";
            this.enterpriseList = [];
            this.getEnterpriseList();
        },
        enterpriseGetUnit(e) {
            this.search_val.tenantId = e;
            this.projectList = [];
            this.selectedValue = "";
            this.sysInstanceIndexUnit = 1;
            if (!e) return;
            this.OrgList();
        },
    },
};